/**
 * $Id: Employee.java 6 2011-03-09 07:12:48Z r.middel@gmail.com $
 *
 * Copyright (C) 2010 mrBussy.eu
 * 
 * WickedHRM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * WickedHRM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.mrbussy.applications.wickedhrm.data;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Locale;

public class Employee extends BaseObject implements Serializable {
	
	private static final String KEY_ATTR_EMPLOYEEID = "employeeid";
	private static final String KEY_ATTR_LASTNAME = "lastname";
	private static final String KEY_ATTR_INITIALS = "initials";
	private static final String KEY_ATTR_FIRSTNAME = "firstname";
	private static final String KEY_ATTR_BIRTHDATE = "birthdate";

	/**
	 * Last generated employee ID
	 */
	private static int lastEmployeeID=0;
	
	/**
	 * Serial number required for unique identifying this class
	 */
	private static final long serialVersionUID = 8828875375503495891L;

	private Collection<Employee> employees=null;
	
	/**
	 * Get the employee number
	 * @return a string representive for the employee number
	 */
	public int getEmployeeID() { return Integer.parseInt(this.getAttribute(KEY_ATTR_EMPLOYEEID)); }
	/**
	 * Set the employee number
	 * @param value the new employee number
	 */
	public void setEmployeeID(int value) { this.setAttribute(KEY_ATTR_EMPLOYEEID, String.valueOf(value)); }
	
	public String getLastname() { return (String)this.getAttribute(KEY_ATTR_LASTNAME); }
	public void setLastname(String value) { this.setAttribute(KEY_ATTR_LASTNAME, value); }
	
	public String getInitials() { return (String)this.getAttribute(KEY_ATTR_INITIALS); }
	public void setInitials(String value) { this.setAttribute(KEY_ATTR_INITIALS, value); }
	
	public String getFirstname() { return (String)this.getAttribute(KEY_ATTR_FIRSTNAME); }
	public void setFirstname(String value) { this.setAttribute(KEY_ATTR_FIRSTNAME, value); }
	
	public Calendar getBirthDate() throws ParseException { return this.getAttributeCalendar(KEY_ATTR_BIRTHDATE); } 
	public void setBirthDate(Calendar value) {
		this.setAttribute(KEY_ATTR_BIRTHDATE, value);
	}
	public void setBirthDate(String value) throws ParseException {
			Calendar birthdate = Calendar.getInstance();
			birthdate.setTime(this.dateFormatter.parse(value));
			this.setBirthDate(birthdate);
	}
	
	public Employee getManager() { return null; }
	public void setManager(Employee value) {}

	public Collection<Employee> getEmployees() { return null; }
	public void addEmployee(Employee emp) {
		this.employees.add(emp);
	}

	/**
	 * Initialize a new employee
	 */
	private Employee() {
		super();
		employees = new ArrayList<Employee>();
	} // Employee
	
	/**
	 * Return string representation for Employee using the specified format.
	 */
	@Override
	public String toString() {
		return this.getLastname() + ", " + this.getFirstname() + (this.getInitials()==null ? " " : this.getInitials());
	} // toString
	
	/**
	 * Return string representation for Employee using the specified format.
	 * 	F	Firstname
	 * 	L	Lastname
	 * 	I	Initials
	 *  B	Birthdate
	 * @param format
	 * @return
	 * @throws ParseException 
	 */
	public String toString(String format) throws ParseException {
		String returnValue = "";

		if(format==null) {
			throw new NullPointerException("format");
		}
		
		for(char c : format.toCharArray()) {
			switch(c) {
				case 'F':
					returnValue = returnValue.concat(this.getFirstname());
					break;
				case 'f':
					returnValue = returnValue.concat(this.getFirstname().toLowerCase());
					break;
				case 'L':
					returnValue = returnValue.concat(this.getLastname());
					break;
				case 'l':
					returnValue = returnValue.concat(this.getLastname().toLowerCase());
					break;
				case 'I':
					returnValue = returnValue.concat(this.getInitials());
					break;
				case 'i':
					returnValue = returnValue.concat(this.getInitials().toLowerCase());
					break;
				case 'B':
					returnValue = returnValue.concat(DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(this.getBirthDate()));
					break;
				default:
					returnValue = returnValue.concat( String.valueOf(c) );
					break;
			}
		}

		return returnValue;
	} // toString(String format)
	
	
	public static int generateEmployeeID() {
		return lastEmployeeID++;
	} // generateEmployeeID()
	
	/**
	 * Create a new employee
	 */
	public static Employee create() {
		Employee newEmp = new Employee();
		newEmp.setEmployeeID(Employee.generateEmployeeID());
		newEmp.setLastname("");
		newEmp.setFirstname("");
		newEmp.setInitials("");
		return newEmp;
	} // create
	/**
	 * Create a new employee
	 */
	public static Employee create(int employeeId) {
		Employee newEmp = Employee.create();
		newEmp.setEmployeeID(employeeId);
		// Max out the last given employeeid
		if(employeeId<lastEmployeeID) lastEmployeeID = employeeId;
		return newEmp;
	} // create(int employeeId)

}	// Class Employee
